package com.ui;

import java.awt.EventQueue;

import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;









import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.https.HttpCore;
import com.https.HttpHeader;
import com.https.UrlConstants;
import com.i18n.Messages;
import com.thread.ConfirmCodeThread;
import com.thread.InitConfig;
import com.thread.LoginThread;
import com.thread.RunThread;
import com.utilities.StringHelper;
import com.utilities.TicketCode;
import com.utilities.TicketHelper;
import com.utilities.TimeUtils;
import com.utilities.ToolHelper;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionListener;

import com.bean.LoginBean;
import com.bean.PassengerBean;
import com.bean.QueryUserBean;
import com.bean.SelectTrainBean;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.events.VerifyEvent;


/**
* @ClassName: MainWindow
* @Description: TODO
* @author alterhu2020@gmail.com
* @date Feb 15, 2014 9:25:20 PM
* 
*/

public class MainWindow {

	
	private static Logger logger=Logger.getLogger(MainWindow.class);
	//此处定义了所有的全局变量
	protected Shell mainshell;
	public Text outputlog;   
	private Text text_username;
	private Text text_password;
	
	
	//httpclient的相关主API
	public  HttpCore client = new HttpCore();
	//验证码存放地址
	public String login_image="C:\\login_passcode.png";   
	public String confirm_image="C:\\confirm_passcode.png";
    //整个过程中的cookies
	public  Map<String, String> cookies = new LinkedHashMap<String, String>();
	//共用页面对象
	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
	private Table table_type;    //列车车座类型列表
	
	private Table table_list;  //列车车次列表
	
	public Text text_from; //出发地  
	public Text text_to;   //目的地
	public DateTime dateTime_date ; //列车出发时间
	public Button button_adult ;
	public Button button_student;
	public String strTicketType="ADULT";
	
	public Text text_inputcode;  //输入的订单确认验证码
	
	public Label lblNewLabel_refreshcode;  //确认订单验证码标签
	public Label lblNewLabel_logincode;  //登录刷新验证码标签
	
	public Button button_login; //登录按钮
	
	public Button btnNewButton_run ; //抢票按钮
	
	public MainWindow window=this;
	public InitConfig config;  //得到全文的配置信息
	
	
	public boolean islogged=false;   //是否登录了12306网站
	
	public boolean isstopped=false;
	public boolean isrunthread=false;
	public boolean inputconfirmcode=false;
	
	
	public Text text_logincode;  //登录中输入的验证码框文本
	public Label lblNewLabel_resultlog; //查询列车的结果显示
	
	//登录的相关用户信心
	public LoginBean loginbean=new LoginBean();
	//public PassengerBean passengers=new PassengerBean(passengername, passengercardid)
	
	public SelectTrainBean selectedtrain=new SelectTrainBean();
	public String longtoken;
	private Table table_passengerslist;
	

	/**
	 * Launch the application.主函数 
	 * @param args
	 */
	public static void main(String[] args) {
		
		//EventQueue.invokeLater(new Runnable() {
		//	public void run() {
			try {
				MainWindow window = new MainWindow();
				window.open();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
	//	}
	  // });
	}

	/**
	 * 第二个方法用户打开窗口Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
	//	logger("我们先初始化下你的机器的环境，以便抢票正常进行...");
		config=new InitConfig(this);
		config.getConfig();
		config.generatePasscode(window);
		
		createContents();
		mainshell.open();
		mainshell.layout();
		mainshell.setMaximized(true);
	    logger("BIGipServerotn 是： "+this.client.BIGipServerotn+",JSESSIONID 是: "+this.client.JSESSIONID);
		while (!mainshell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		mainshell = new Shell();
		mainshell.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/logo.png"));
		mainshell.setSize(974, 894);
		mainshell.setText(Messages.MainWindow_mainshell_text_2+Messages.version_number);
		mainshell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Composite maincomposite = new Composite(mainshell, SWT.NONE);
		maincomposite.setLayout(new FormLayout());
		
		//选择不同的tab进行数据
		final TabFolder tabFolder = new TabFolder(maincomposite, SWT.NONE);
		tabFolder.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(tabFolder.getSelectionIndex()==1){
					
					if(window.islogged&&(table_passengerslist.getItemCount()==0)){ //如果已经登录了
						
						logger("试图尝试获得当前登录用户的关联联系人信息......");
						table_passengerslist.removeAll();
						table_passengerslist.setRedraw(true);
						try {
							String msg=client.postRequest(UrlConstants.URL_PASSENGER, null, HttpHeader.getPassengerList(), cookies);
							
							
							JSONObject content=JSONObject.fromObject(msg);
							JSONObject data=content.getJSONObject("data");
							JSONArray normal_passengers=data.getJSONArray("normal_passengers");
							int passengersize=normal_passengers.size();
							
							
							for(int k=0;k<passengersize;k++){
								JSONObject passenger=normal_passengers.getJSONObject(k);
								
								//String[] itemvalue=new String[11];
								String passenger_name=passenger.getString("passenger_name");
								//String sex_name="";
								//if(passenger.getString("passenger_id_type_code").equals("1")){ //一代身份证
								String	sex_name=passenger.optString("sex_name","");
								//}
								//String 
								String born_date=passenger.getString("born_date");
								String country_code=passenger.getString("country_code");
								String passenger_id_type_code=passenger.getString("passenger_id_type_code");
								String passenger_id_no=passenger.getString("passenger_id_no");
								String passenger_type=passenger.getString("passenger_type");
								//String passenger_flag=passenger.getString("passenger_flag");
								String mobile_no=passenger.getString("mobile_no");
								String email=passenger.getString("email");
								String address=passenger.getString("address");
								
								//String 
								String[] itemvalue=new String[]{"",passenger_name,sex_name,born_date,country_code,passenger_type,passenger_id_no,passenger_id_type_code,mobile_no,email,address};					
								TableItem item = new TableItem(table_passengerslist, SWT.NULL);
								     // item.setText("Item " + loopIndex);
								item.setText(itemvalue);
								item.setChecked(false);
								     
							}
						} catch (KeyManagementException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (NoSuchAlgorithmException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}else{
						logger("已经获得了对应的用户的所有联系人信息，可能你没有登录12306或者是已经加载了所有关联的联系人信息");
					}
				}
				
			}
		});
		FormData fd_tabFolder = new FormData();
		fd_tabFolder.right = new FormAttachment(100);
		fd_tabFolder.bottom = new FormAttachment(60);
		fd_tabFolder.top = new FormAttachment(0, 3);
		fd_tabFolder.left = new FormAttachment(0, 5);
		tabFolder.setLayoutData(fd_tabFolder);
		
		Composite composite_run = new Composite(maincomposite, SWT.NONE);
		FormData fd_composite_run = new FormData();
		fd_composite_run.left = new FormAttachment(0);
		fd_composite_run.bottom = new FormAttachment(70);
		fd_composite_run.top = new FormAttachment(tabFolder);
		fd_composite_run.right = new FormAttachment(100);
		composite_run.setLayoutData(fd_composite_run);
		formToolkit.adapt(composite_run);
		formToolkit.paintBordersFor(composite_run);
		
		btnNewButton_run = new Button(composite_run, SWT.NONE);
		btnNewButton_run.addSelectionListener(new startRunListener()); //抢票进程
		btnNewButton_run.setBounds(27, 13, 75, 36);
		formToolkit.adapt(btnNewButton_run, true, true);
		btnNewButton_run.setText(Messages.MainWindow_btnNewButton_runnow);
		
		Label lblNewLabel_passcode = new Label(composite_run, SWT.NONE);
		lblNewLabel_passcode.setBounds(127, 11, 86, 15);
		formToolkit.adapt(lblNewLabel_passcode, true, true);
		lblNewLabel_passcode.setText(Messages.MainWindow_lblNewLabel_confirmtext);
		
		text_inputcode = new Text(composite_run, SWT.BORDER);
		text_inputcode.addVerifyListener(new VerifyListener() {
			public void verifyText(VerifyEvent e) {				
				 String currentText = ((Text)e.widget).getText();
			     if(currentText.length()==3){
			    	 System.out.println("检查输入的字符串长度是否是4个，如果是4个就进行验证码校验操作，现在输入的是4个字符串了");
			    	 window.logger("检查输入的字符串长度是否是4个，如果是4个就进行验证码校验操作，现在输入的是4个字符串了");
			    	 if(window.islogged){
			    	      new ConfirmCodeThread(strTicketType,window).start();
			    	 }
			     }
			}
		});
	
		text_inputcode.setText(Messages.MainWindow_text_2_text);
		text_inputcode.setBounds(328, 8, 96, 28);
		formToolkit.adapt(text_inputcode, true, true);
		
		
		//自动录入验证码
		final Button btnCheckButton_manual = new Button(composite_run, SWT.CHECK);
		//btnCheckButton_manual.setSelection(true);
		btnCheckButton_manual.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			     boolean isauto=btnCheckButton_manual.getSelection();
			      //自动进行输入验证码操作
			     if(isauto){
			    	 
			     }
			     //手工进行输入验证码
			     else{
			    	 
			     }
			}
		});
		btnCheckButton_manual.setBounds(451, 4, 139, 16);
		formToolkit.adapt(btnCheckButton_manual, true, true);
		btnCheckButton_manual.setText(Messages.MainWindow_btnCheckButton_text_1);
		
		Link link_nopay = new Link(composite_run, SWT.NONE);
		link_nopay.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				org.eclipse.swt.program.Program.launch(e.text);
			}
		});
		link_nopay.setBounds(691, 11, 96, 25);
		formToolkit.adapt(link_nopay, true, true);
		link_nopay.setText(Messages.MainWindow_link_nopay);
		
		lblNewLabel_refreshcode = new Label(composite_run, SWT.NONE);
		lblNewLabel_refreshcode.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				
				//重新生成验证码
				org.eclipse.swt.graphics.Image imageobject=lblNewLabel_refreshcode.getImage();
				//已经生成了了验证码以前
				if(imageobject!=null){
					   int codeindex=TimeUtils.randomnumber(100, 10000);
				       String url=UrlConstants.URL_CONFIRM_PASSCODE+codeindex;
				       try {
						   window.client.getPassCode(url,window.confirm_image, HttpHeader.getPassCode(true), window.cookies);
						   SWTResourceManager.disposeImages();
						   window.lblNewLabel_refreshcode.setImage(SWTResourceManager.getImage(window.confirm_image));
						   window.lblNewLabel_refreshcode.redraw();
						   window.text_inputcode.setText("");
						   window.logger("成功生成了确认订单的验证码！");
					} catch (KeyManagementException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSuchAlgorithmException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}	
				     
				}
				
			}
		});
		lblNewLabel_refreshcode.setImage(null);
		lblNewLabel_refreshcode.setBounds(219, 11, 96, 49);
		formToolkit.adapt(lblNewLabel_refreshcode, true, true);
		lblNewLabel_refreshcode.setText(Messages.MainWindow_lblNewLabel_confirmcode);
		
		Group group_output = new Group(maincomposite, SWT.NONE);
		group_output.setLayout(new FillLayout(SWT.HORIZONTAL));
		FormData fd_group_output = new FormData();
		fd_group_output.top = new FormAttachment(composite_run);
		
		Button button_clear = new Button(composite_run, SWT.NONE);
		button_clear.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				outputlog.setText("");
			}
		});
		button_clear.setBounds(873, 49, 75, 25);
		formToolkit.adapt(button_clear, true, true);
		button_clear.setText(Messages.MainWindow_button_text_1);
		fd_group_output.bottom = new FormAttachment(100);
		fd_group_output.right = new FormAttachment(100);
		fd_group_output.left = new FormAttachment(0);
		group_output.setLayoutData(fd_group_output);
		group_output.setText(Messages.MainWindow_group_output);
		
		outputlog = new Text(group_output, SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
		outputlog.setText(Messages.MainWindow_text_outputlog);
		
		TabItem tbtmNewItem_login = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem_login.setText(Messages.MainWindow_tbtmNewItem_firststep);
		
		Group group_login = new Group(tabFolder, SWT.NONE);
		group_login.setText(Messages.MainWindow_group_12306_login);
		tbtmNewItem_login.setControl(group_login);
		group_login.setLayout(new FormLayout());
		
		Label label_username = new Label(group_login, SWT.NONE);
		FormData fd_label_username = new FormData();
		label_username.setLayoutData(fd_label_username);
		label_username.setText(Messages.MainWindow_label_user);
		
		text_username = new Text(group_login, SWT.BORDER);
		text_username.setText(Messages.MainWindow_from);
		FormData fd_text_username = new FormData();
		fd_text_username.top = new FormAttachment(0, 41);
		fd_text_username.right = new FormAttachment(100, -302);
		fd_text_username.left = new FormAttachment(label_username, 32);
		text_username.setLayoutData(fd_text_username);
		
		Label label_password = new Label(group_login, SWT.NONE);
		fd_label_username.bottom = new FormAttachment(label_password, -32);
		fd_label_username.left = new FormAttachment(label_password, 0, SWT.LEFT);
		FormData fd_label_password = new FormData();
		fd_label_password.top = new FormAttachment(0, 88);
		fd_label_password.left = new FormAttachment(0, 44);
		label_password.setLayoutData(fd_label_password);
		label_password.setText(Messages.MainWindow_label_password);
		
		text_password = new Text(group_login, SWT.BORDER | SWT.PASSWORD);
		fd_text_username.bottom = new FormAttachment(text_password, -23);
		//fd_text_username.bottom = new FormAttachment(text_password, -6);
		//fd_text_username.left = new FormAttachment(text_password, 0, SWT.LEFT);
		text_password.setText(Messages.MainWindow_text_1_text);
		FormData fd_text_password = new FormData();
		fd_text_password.top = new FormAttachment(label_password, -3, SWT.TOP);
		fd_text_password.left = new FormAttachment(text_username, 0, SWT.LEFT);
		fd_text_password.right = new FormAttachment(100, -302);
		text_password.setLayoutData(fd_text_password);
		
		Label label = new Label(group_login, SWT.NONE);
		FormData fd_label = new FormData();
		fd_label.top = new FormAttachment(label_password, 49);
		fd_label.left = new FormAttachment(0, 44);
		label.setLayoutData(fd_label);
		label.setText(Messages.MainWindow_label_text);
		
	    lblNewLabel_logincode = new Label(group_login, SWT.NONE);
		lblNewLabel_logincode.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				config.generatePasscode(window);
				SWTResourceManager.disposeImages();
				lblNewLabel_logincode.setImage(SWTResourceManager.getImage(login_image));
				lblNewLabel_logincode.redraw();
				logger("已经为你重新刷新了验证码");
			}
		});
		lblNewLabel_logincode.setImage(SWTResourceManager.getImage("C:\\\\login_passcode.png"));
		//lblNewLabel_refreshcode.setImage(SWTResourceManager.getImage(login_image));
		//lblNewLabel_refreshcode.redraw();
		
		FormData fd_lblNewLabel_logincode = new FormData();
		fd_lblNewLabel_logincode.left = new FormAttachment(text_username, -14, SWT.LEFT);
		fd_lblNewLabel_logincode.top = new FormAttachment(text_password, 15);
		lblNewLabel_logincode.setLayoutData(fd_lblNewLabel_logincode);
		lblNewLabel_logincode.setText(Messages.MainWindow_lblNewLabel_text);
		
		Button button_manualinput = new Button(group_login, SWT.CHECK);
		fd_lblNewLabel_logincode.right = new FormAttachment(100, -736);
		FormData fd_button_manualinput = new FormData();
		fd_button_manualinput.top = new FormAttachment(label, -1, SWT.TOP);
		//fd_button_manualinput.top = new FormAttachment(label, -1, SWT.TOP);
		button_manualinput.setLayoutData(fd_button_manualinput);
		button_manualinput.setText(Messages.MainWindow_button_text);
		//登录线程
		button_login = new Button(group_login, SWT.NONE);
		
		fd_lblNewLabel_logincode.bottom = new FormAttachment(button_login, -22);
		button_login.addMouseListener(new LoginListener());
		FormData fd_button_login = new FormData();
		fd_button_login.top = new FormAttachment(0, 224);
		fd_button_login.right = new FormAttachment(100, -327);
		fd_button_login.left = new FormAttachment(0, 166);
		button_login.setLayoutData(fd_button_login);
		button_login.setText(Messages.MainWindow_button_1_text);
		
		Label label_1 = new Label(group_login, SWT.NONE);
		FormData fd_label_1 = new FormData();
		fd_label_1.right = new FormAttachment(button_login, 146, SWT.RIGHT);
		fd_label_1.bottom = new FormAttachment(button_login, 143, SWT.BOTTOM);
		fd_label_1.top = new FormAttachment(button_login, 46);
		fd_label_1.left = new FormAttachment(0, 44);
		label_1.setLayoutData(fd_label_1);
		label_1.setText(Messages.MainWindow_label_attention);
		
		text_logincode = new Text(group_login, SWT.BORDER);
		fd_button_manualinput.left = new FormAttachment(text_logincode, 19);
		text_logincode.addKeyListener(new LoginListener());
		
		text_logincode.setText(Messages.MainWindow_text_text);
		FormData fd_text_logincode = new FormData();
		fd_text_logincode.right = new FormAttachment(100, -631);
		fd_text_logincode.left = new FormAttachment(lblNewLabel_logincode, 3);
		fd_text_logincode.top = new FormAttachment(label, -4, SWT.TOP);
		fd_text_logincode.bottom = new FormAttachment(label, 29);
		text_logincode.setLayoutData(fd_text_logincode);
		formToolkit.adapt(text_logincode, true, true);
		group_login.setTabList(new Control[]{text_username, text_password, text_logincode, button_manualinput, button_login});
		
		TabItem tbtmNewItem_passengerlist = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem_passengerlist.setText(Messages.MainWindow_tbtmNewItem_text_1);
		
		Composite composite_passengermanager = new Composite(tabFolder, SWT.NONE);
		tbtmNewItem_passengerlist.setControl(composite_passengermanager);
		formToolkit.paintBordersFor(composite_passengermanager);
		composite_passengermanager.setLayout(new FormLayout());
		
		Group group_operation = new Group(composite_passengermanager, SWT.NONE);
		group_operation.setText(Messages.MainWindow_group_1_text);
		FormData fd_group_operation = new FormData();
		fd_group_operation.bottom = new FormAttachment(10);
		fd_group_operation.top = new FormAttachment(0);
		fd_group_operation.right = new FormAttachment(100);
		fd_group_operation.left = new FormAttachment(0);
		group_operation.setLayoutData(fd_group_operation);
		formToolkit.adapt(group_operation);
		formToolkit.paintBordersFor(group_operation);
		
		Group group_list = new Group(composite_passengermanager, SWT.NONE);
		group_list.setText(Messages.MainWindow_group_text);
		FormData fd_group_list = new FormData();
		fd_group_list.bottom = new FormAttachment(100);
		fd_group_list.top = new FormAttachment(group_operation);
		
		Button btnNewButton_add = new Button(group_operation, SWT.NONE);
		btnNewButton_add.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				AddPassenger add=new AddPassenger(mainshell);
				add.open();
			}
		});
		btnNewButton_add.setBounds(127, 10, 96, 25);
		formToolkit.adapt(btnNewButton_add, true, true);
		btnNewButton_add.setText(Messages.MainWindow_btnNewButton_text_3);
		
		Button btnNewButton_delete = new Button(group_operation, SWT.NONE);
		btnNewButton_delete.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnNewButton_delete.setBounds(355, 10, 108, 25);
		formToolkit.adapt(btnNewButton_delete, true, true);
		btnNewButton_delete.setText(Messages.MainWindow_btnNewButton_1_text);
		group_list.setLayout(new FillLayout(SWT.HORIZONTAL));
		fd_group_list.right = new FormAttachment(100);
		fd_group_list.left = new FormAttachment(0);
		group_list.setLayoutData(fd_group_list);
		formToolkit.adapt(group_list);
		formToolkit.paintBordersFor(group_list);
		
		table_passengerslist = formToolkit.createTable(group_list, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION | SWT.MULTI);
		table_passengerslist.setToolTipText("我的关联旅客信息");
		table_passengerslist.setSelection(2);
		formToolkit.paintBordersFor(table_passengerslist);
		table_passengerslist.setHeaderVisible(true);
		table_passengerslist.setLinesVisible(true);
		
		final TableColumn tableColumn_passenger_checkall = new TableColumn(table_passengerslist, SWT.NONE);
		tableColumn_passenger_checkall.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				boolean checkBoxFlag = false;
		        for (int i = 0; i < table_passengerslist.getItemCount(); i++) {
		            if (table_passengerslist.getItems()[i].getChecked()) {
		                checkBoxFlag = true;
		                break;
		            }
		        }

		        if (checkBoxFlag) {
		            for (int m = 0; m < table_passengerslist.getItemCount(); m++) {
		            	table_list.getItems()[m].setChecked(false);
		            	tableColumn_passenger_checkall.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/uncheckbox.ico"));

		            	table_passengerslist.deselectAll();

		            }
		        } else {
		            for (int m = 0; m < table_passengerslist.getItemCount(); m++) {
		            	table_passengerslist.getItems()[m].setChecked(true);
		            	tableColumn_passenger_checkall.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/checkbox.ico"));

		            	table_passengerslist.selectAll();
		            }
		        }
				
			}
		});
		tableColumn_passenger_checkall.setWidth(50);
		tableColumn_passenger_checkall.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/checkbox.ico"));
		
		TableColumn tableColumn_name = new TableColumn(table_passengerslist, SWT.NONE);
		tableColumn_name.setWidth(100);
		tableColumn_name.setText("联系人姓名");
		
		TableColumn tableColumn_sex = new TableColumn(table_passengerslist, SWT.NONE);
		tableColumn_sex.setWidth(40);
		tableColumn_sex.setText("性别");
		
		TableColumn tableColumn_birth = new TableColumn(table_passengerslist, SWT.NONE);
		tableColumn_birth.setWidth(97);
		tableColumn_birth.setText("出生日期");
		
		TableColumn tableColumn_code = new TableColumn(table_passengerslist, SWT.NONE);
		tableColumn_code.setWidth(60);
		tableColumn_code.setText("国家代码");
		
		TableColumn tableColumn_id = new TableColumn(table_passengerslist, SWT.NONE);
		tableColumn_id.setWidth(55);
		tableColumn_id.setText("乘客ID");
		
		TableColumn tableColumn_cardid = new TableColumn(table_passengerslist, SWT.NONE);
		tableColumn_cardid.setWidth(150);
		tableColumn_cardid.setText("身份证号码");
		
		TableColumn tableColumn_cardtype = new TableColumn(table_passengerslist, SWT.NONE);
		tableColumn_cardtype.setWidth(100);
		tableColumn_cardtype.setText("身份证类型");
		
		TableColumn tableColumn_phone = new TableColumn(table_passengerslist, SWT.NONE);
		tableColumn_phone.setWidth(120);
		tableColumn_phone.setText("手机号码");
		
		TableColumn tableColumn_email = new TableColumn(table_passengerslist, SWT.NONE);
		tableColumn_email.setWidth(130);
		tableColumn_email.setText("邮件");
		
		TableColumn tableColumn_address = new TableColumn(table_passengerslist, SWT.NONE);
		tableColumn_address.setWidth(140);
		tableColumn_address.setText("地址");
		
		TabItem tbtmNewItem_traininfo = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem_traininfo.setText(Messages.MainWindow_tbtmNewItem_thirdstep);
		
		Composite composite = new Composite(tabFolder, SWT.NONE);
		tbtmNewItem_traininfo.setControl(composite);
		formToolkit.paintBordersFor(composite);
		FormLayout fl_composite = new FormLayout();
		fl_composite.marginWidth = 5;
		fl_composite.spacing = 5;
		composite.setLayout(fl_composite);
		
		Group group_conditions = new Group(composite, SWT.NONE);
		group_conditions.setText(Messages.MainWindow_group_2_text_3);
		FormData fd_group_conditions = new FormData();
		fd_group_conditions.right = new FormAttachment(100);
		fd_group_conditions.left = new FormAttachment(0);
		fd_group_conditions.top = new FormAttachment(0);
		fd_group_conditions.bottom = new FormAttachment(15);
		group_conditions.setLayoutData(fd_group_conditions);
		formToolkit.adapt(group_conditions);
		formToolkit.paintBordersFor(group_conditions);
		
		Group group_result = new Group(composite, SWT.NONE);
		group_result.setText(Messages.MainWindow_group_3_text);
		FormData fd_group_result = new FormData();
		fd_group_result.bottom = new FormAttachment(100);
		fd_group_result.top = new FormAttachment(group_conditions);
		
		Label label_from = new Label(group_conditions, SWT.NONE);
		label_from.setBounds(10, 20, 55, 15);
		formToolkit.adapt(label_from, true, true);
		label_from.setText(Messages.MainWindow_label_from);
		
		text_from = new Text(group_conditions, SWT.BORDER);
		text_from.setText(Messages.MainWindow_text_from);
		text_from.setBounds(87, 14, 73, 21);
		formToolkit.adapt(text_from, true, true);
		
		Label label_to = new Label(group_conditions, SWT.NONE);
		label_to.setBounds(194, 20, 55, 15);
		formToolkit.adapt(label_to, true, true);
		label_to.setText(Messages.MainWindow_label_to);
		
		text_to = new Text(group_conditions, SWT.BORDER);
		text_to.setText(Messages.MainWindow_text_to);
		text_to.setBounds(274, 14, 73, 24);
		formToolkit.adapt(text_to, true, true);
		
		dateTime_date = new DateTime(group_conditions, SWT.BORDER);
		dateTime_date.setBounds(474, 14, 80, 24);
		formToolkit.adapt(dateTime_date);
		formToolkit.paintBordersFor(dateTime_date);
		
		Label label_date = new Label(group_conditions, SWT.NONE);
		label_date.setBounds(385, 20, 55, 15);
		formToolkit.adapt(label_date, true, true);
		label_date.setText(Messages.MainWindow_label_4_text_1);
		
		Button button_query = new Button(group_conditions, SWT.NONE);
		button_query.addSelectionListener(new queryTrainListener());
		button_query.setBounds(654, 15, 75, 25);
		formToolkit.adapt(button_query, true, true);
		button_query.setText(Messages.MainWindow_button_search);
		
		lblNewLabel_resultlog = new Label(group_conditions, SWT.NONE);
		lblNewLabel_resultlog.setBounds(735, 10, 190, 50);
		formToolkit.adapt(lblNewLabel_resultlog, true, true);
		lblNewLabel_resultlog.setText(Messages.MainWindow_lblNewLabel_text_1);
		
		button_adult = new Button(group_conditions, SWT.RADIO);
		button_adult.setSelection(true);
		button_adult.setBounds(560, 14, 90, 16);
		formToolkit.adapt(button_adult, true, true);
		button_adult.setText(Messages.MainWindow_button_text_3);
		
		button_student = new Button(group_conditions, SWT.RADIO);
		button_student.setBounds(560, 44, 90, 16);
		formToolkit.adapt(button_student, true, true);
		button_student.setText(Messages.MainWindow_button_1_text_1);
		
		group_result.setLayout(new FillLayout(SWT.HORIZONTAL));
		fd_group_result.right = new FormAttachment(100);
		fd_group_result.left = new FormAttachment(0);
		group_result.setLayoutData(fd_group_result);
		formToolkit.adapt(group_result);
		formToolkit.paintBordersFor(group_result);
		
		table_list = new Table(group_result, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
		table_list.setLinesVisible(true);
		table_list.setHeaderVisible(true);
		formToolkit.adapt(table_list);
		formToolkit.paintBordersFor(table_list);
		
		final TableColumn tableColumn_train_checkall = new TableColumn(table_list, SWT.NONE);
		//全选响应事件
		tableColumn_train_checkall.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				boolean checkBoxFlag = false;
		        for (int i = 0; i < table_list.getItemCount(); i++) {
		            if (table_list.getItems()[i].getChecked()) {
		                checkBoxFlag = true;
		                break;
		            }
		        }

		        if (checkBoxFlag) {
		            for (int m = 0; m < table_list.getItemCount(); m++) {
		            	table_list.getItems()[m].setChecked(false);
		            	tableColumn_train_checkall.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/uncheckbox.ico"));

		            	table_list.deselectAll();

		            }
		        } else {
		            for (int m = 0; m < table_list.getItemCount(); m++) {
		            	table_list.getItems()[m].setChecked(true);
		            	tableColumn_train_checkall.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/checkbox.ico"));

		            	table_list.selectAll();
		            }
		        }
			}
		});
		tableColumn_train_checkall.setWidth(50);
		tableColumn_train_checkall.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/checkbox.ico"));
		
		TableColumn tableColumn_train_trainname = new TableColumn(table_list, SWT.NONE);
		tableColumn_train_trainname.setWidth(100);
		tableColumn_train_trainname.setText("车次");
		
		TableColumn tableColumn_train_trainfromto = new TableColumn(table_list, SWT.NONE);
		tableColumn_train_trainfromto.setWidth(100);
		tableColumn_train_trainfromto.setText("出发地目的地");
		
		TableColumn tableColumn_train_startend = new TableColumn(table_list, SWT.NONE);
		tableColumn_train_startend.setWidth(211);
		tableColumn_train_startend.setText("出发时间和到达时间");
		
		TableColumn tableColumn_long = new TableColumn(table_list, SWT.NONE);
		tableColumn_long.setWidth(50);
		tableColumn_long.setText("历时");
		
		TableColumn tableColumn_securstr = new TableColumn(table_list, SWT.NONE);
		tableColumn_securstr.setWidth(100);
		tableColumn_securstr.setText("安全字符串");
		
		TableColumn tblclmnNewColumn_yz = new TableColumn(table_list, SWT.NONE);
		tblclmnNewColumn_yz.setWidth(69);
		tblclmnNewColumn_yz.setText(Messages.MainWindow_tblclmnNewColumn_text);
		
		TableColumn tblclmnyn = new TableColumn(table_list, SWT.LEFT);
		tblclmnyn.setWidth(302);
		tblclmnyn.setText(Messages.MainWindow_tblclmnyn_text);
		
		TabItem tbtmNewItem_seat = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem_seat.setText(Messages.MainWindow_tbtmNewItem_text);
		
		Group group_type = new Group(tabFolder, SWT.NONE);
		group_type.setText("座位类型");
		tbtmNewItem_seat.setControl(group_type);
		formToolkit.paintBordersFor(group_type);
		group_type.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		table_type = new Table(group_type, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
		table_type.setLinesVisible(true);
		table_type.setHeaderVisible(true);
		formToolkit.adapt(table_type);
		formToolkit.paintBordersFor(table_type);
		
		final TableColumn tableColumn_checkall = new TableColumn(table_type, SWT.NONE);
		tableColumn_checkall.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				boolean checkBoxFlag = false;
		        for (int i = 0; i < table_type.getItemCount(); i++) {
		            if (table_type.getItems()[i].getChecked()) {
		                checkBoxFlag = true;
		                break;
		            }
		        }

		        if (checkBoxFlag) {
		            for (int m = 0; m < table_type.getItemCount(); m++) {
		            	table_type.getItems()[m].setChecked(false);
		            	tableColumn_checkall.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/uncheckbox.ico"));

		            	table_type.deselectAll();

		            }
		        } else {
		            for (int m = 0; m < table_type.getItemCount(); m++) {
		            	table_type.getItems()[m].setChecked(true);
		            	tableColumn_checkall.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/checkbox.ico"));

		            	table_type.selectAll();
		            }
		        }
			}
		});
		tableColumn_checkall.setWidth(50);
		tableColumn_checkall.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/checkbox.ico"));
		
		TableColumn tableColumn_type = new TableColumn(table_type, SWT.LEFT);
		tableColumn_type.setWidth(174);
		tableColumn_type.setText("类型");
		
		TableColumn tableColumn_typecode = new TableColumn(table_type, SWT.CENTER);
		tableColumn_typecode.setWidth(107);
		tableColumn_typecode.setText(Messages.MainWindow_tableColumn_longcode);

		
		TableColumn tableColumn_shortcode = new TableColumn(table_type, SWT.CENTER);
		tableColumn_shortcode.setWidth(107);
		tableColumn_shortcode.setText(Messages.MainWindow_tableColumn_shortcode);
		
		TableItem tableItem_yz = new TableItem(table_type, 0);
		tableItem_yz.setText(new String[] {"", "硬座", "yz_num", "1"});
		
		
		tableItem_yz.setChecked(false);
		//tableItem_yz.setText("硬座");
		
		TableItem tableItem_rz = new TableItem(table_type, 0);
		tableItem_rz.setText(new String[] {"", "软座", "rz_num", "2"});
		tableItem_rz.setChecked(false);
		
		TableItem tableItem_yw = new TableItem(table_type, 0);
		tableItem_yw.setText(new String[] {"", "硬卧", "yw_num", "3"});
		tableItem_yw.setChecked(false);
		
		TableItem tableItem_rw = new TableItem(table_type, 0);
		tableItem_rw.setText(new String[] {"", "软卧", "rw_num", "4"});
		tableItem_rw.setChecked(false);
		
		TableItem tableItem_gjrw = new TableItem(table_type, 0);
		tableItem_gjrw.setText(new String[] {"", "高级软卧", "gr_num", "6"});
		tableItem_gjrw.setChecked(false);
		
		TableItem tableItem_yd = new TableItem(table_type, 0);
		tableItem_yd.setText(new String[] {"", "一等座", "zy_num", "M"});
		tableItem_yd.setChecked(false);
		
		TableItem tableItem_ed = new TableItem(table_type, 0);
		tableItem_ed.setText(new String[] {"", "二等座", "ze_num", "O"});
		tableItem_ed.setChecked(false);
		
		TableItem tableItem_td = new TableItem(table_type, 0);
		tableItem_td.setText(new String[] {"", "特等座", "tz_num", "P"});
		tableItem_td.setChecked(false);
		
		TableItem tableItem_sw = new TableItem(table_type, 0);
		tableItem_sw.setText(new String[] {"", "商务座", "swz_num", "9"});
		tableItem_sw.setChecked(false);
		
		TableItem tableItem_wz = new TableItem(table_type, 0);
		tableItem_wz.setText(new String[] {"", "无座", "wz_num", "WZ"});
		tableItem_wz.setChecked(false);
		
	
		
		Menu menu = new Menu(mainshell, SWT.BAR);
		mainshell.setMenuBar(menu);
		
		MenuItem mntmNewSubmenu = new MenuItem(menu, SWT.CASCADE);
		mntmNewSubmenu.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/settings.ico"));
		mntmNewSubmenu.setText(Messages.MainWindow_mntmNewSubmenu_text);
		
		Menu menu_options = new Menu(mntmNewSubmenu);
		mntmNewSubmenu.setMenu(menu_options);
		
		MenuItem mntmNewItem_options = new MenuItem(menu_options, SWT.NONE);
	//	mntmNewItem_options.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/preferences_system.ico"));
		mntmNewItem_options.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				try{
					SettingsWindow settings=new SettingsWindow(mainshell);
					settings.open();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
			}
		});
		mntmNewItem_options.setText(Messages.MainWindow_mntmNewItem_text);
		
		MenuItem mntmNewSubmenu_help = new MenuItem(menu, SWT.CASCADE);
		mntmNewSubmenu_help.setImage(SWTResourceManager.getImage(MainWindow.class, "/com/images/help.ico"));
		mntmNewSubmenu_help.setText(Messages.MainWindow_mntmNewSubmenu_help);
		
		Menu menu_help = new Menu(mntmNewSubmenu_help);
		mntmNewSubmenu_help.setMenu(menu_help);
		//访问网站主页
		MenuItem mntmNewItem_visitus = new MenuItem(menu_help, SWT.NONE);
		mntmNewItem_visitus.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				org.eclipse.swt.program.Program.launch("http://www.baidu.com");
			}
		});
		mntmNewItem_visitus.setText(Messages.MainWindow_mntmNewItem_visitus);
		
		MenuItem mntmNewItem_update = new MenuItem(menu_help, SWT.NONE);
		mntmNewItem_update.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try{
					NewUpdates news=new NewUpdates(mainshell);
					news.open();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
			}
		});
		mntmNewItem_update.setText(Messages.MainWindow_mntmNewItem_text_2);
		
		MenuItem menuItem_separator = new MenuItem(menu_help, SWT.SEPARATOR);
		menuItem_separator.setText(Messages.MainWindow_other_text);
		
		
		//软件说明信息
		MenuItem mntmNewItem_aboutus = new MenuItem(menu_help, SWT.NONE);
		mntmNewItem_aboutus.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
					try{
					AboutIT about=new AboutIT(mainshell);
					about.open();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
			}
		});
		mntmNewItem_aboutus.setText(Messages.MainWindow_mntmNewItem_aboutus);

	}

	
	/*
	 * 日志输出方法
	 * 
	 */
	public void logger(final String message){
		 final String currenttime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a").format(Calendar.getInstance().getTime());
		 
		 Display.getDefault().asyncExec(new Runnable() 
		 { 
		 public void run() 
		 { 
			 outputlog.append(currenttime+"  "+message+"\n");
		 } 
		 });
	}
	
	
	/**
	 * GET cookie Map
	 * 
	 * @throws UnsupportedEncodingException
	 */
	public void getCookie(Map<String, String> cookies,String from,String to,String traindate) {
		String fromCode = TicketHelper.getCityCode(TicketHelper.convertStation(from));
		String fromstation=ToolHelper.getUnicode(from, fromCode);
		String toCode = TicketHelper.getCityCode(TicketHelper.convertStation(to));
		String tostation=ToolHelper.getUnicode(to, toCode);
		//ticksearch.setFrom_station(formCode);
		cookies.put("_jc_save_fromDate",traindate);
		cookies.put("_jc_save_fromStation", fromstation);
		cookies.put("_jc_save_toDate", traindate);
		cookies.put("_jc_save_toStation", tostation);
		cookies.put("_jc_save_wfdc_flag", "dc");
	}

	/** 
	* @return loginbean 
	*/ 
	
	/*
	 * 点击登录按钮的响应事件
	 */
	
	class LoginListener implements MouseListener,org.eclipse.swt.events.KeyListener{

		@Override
		public void mouseDoubleClick(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseDown(MouseEvent e) {
			// TODO Auto-generated method stub
			//Button loginbtn=(Button) e.getSource();
			//logger("text is:"+loginbtn.getText().trim());
		//	boolean isinput=true;
			String username=text_username.getText().trim();
			String password=text_password.getText().trim();
			String passcode=text_logincode.getText().trim();
			loginbean.setUsername(username);
			loginbean.setPassword(password);
			loginbean.setPasscode(passcode);
			
			if(loginbean.getUsername().equals("")||loginbean.getPassword().equals("")||loginbean.getPasscode().equals("")){
				logger("请输入用户名，密码，验证码");
				return;
			}
			
				logger("输入的用户名为: "+username+",密码为： "+password+"，验证码为："+passcode+",正在进行连接登陆中.......");
				LoginThread login=new LoginThread(loginbean,window);
				login.start();
								
		
			//loginbtn.setText("登录");
		}

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			if(e.keyCode==SWT.CR){
				
				String username=text_username.getText().trim();
				String password=text_password.getText().trim();
				String passcode=text_logincode.getText().trim();
				loginbean.setUsername(username);
				loginbean.setPassword(password);
				loginbean.setPasscode(passcode);
				
				if(loginbean.getUsername().equals("")||loginbean.getPassword().equals("")||loginbean.getPasscode().equals("")){
					logger("请输入用户名，密码，验证码");
					return;
				}
				
					logger("输入的用户名为: "+username+",密码为： "+password+"，验证码为："+passcode+",正在进行连接登陆中.......");
					LoginThread login=new LoginThread(loginbean,window);
					login.start();
					//logger("登录12306已经完成了，恭喜你你可以抢票了！");
			}
		}

		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseUp(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

	
		
		
	}
	
	/*
	 * 查询列车车次的线程
	 */
	
	class queryTrainListener implements SelectionListener{

		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			// TODO Auto-generated method stub
			Button querybtn=(Button) e.getSource();
			String from=text_from.getText().trim();
			String to=text_to.getText().trim();
			Calendar instance = Calendar.getInstance();
			instance.set(Calendar.DAY_OF_MONTH, dateTime_date.getDay());
			instance.set(Calendar.MONTH, dateTime_date.getMonth());
			instance.set(Calendar.YEAR, dateTime_date.getYear());
			String traindate = new SimpleDateFormat("yyyy-MM-dd").format(instance.getTime());
			
			boolean isadult=button_adult.getSelection();		
			if(isadult){
				strTicketType="ADULT";
			}else{
				strTicketType="0X00";
			}
			
			logger("正在查询对应的当前设置时间的列车车次信息");
			table_list.removeAll();
			table_list.setRedraw(true);
			//String traindate=dateTime_traindate.getYear()+"-"+(dateTime_traindate.getMonth()+1)+"-"+dateTime_traindate.getDay();
			logger("查询此次车次: "+from+" 到  "+to+",列车出发时间为: "+traindate);
			if(!from.equals("")&&!to.equals("")){
				logger("开始查询对应的列车班次......");
				//获得cookies
				cookies.clear();
				getCookie(cookies, from, to, traindate);
				logger("本次查询的Cookies 是："+cookies.toString());
				querybtn.setText("正在查询中...");
				querybtn.setEnabled(false);
				//将查询的结果显示在列表中
				try {
					String jsoncontent=com.utilities.TrainUtility.searchTicket(from, to, traindate,strTicketType, window);
									
					if(jsoncontent==null){
						logger("很抱歉，按您的查询条件，当前未找到从"+from+"到"+to+" 的列车。您可以使用出行向导中的中转查询功能 查询换乘车次。");
						
						querybtn.setText(Messages.MainWindow_button_search);
						lblNewLabel_resultlog.setText("查询结果：很抱歉，按您的查询条件，\n当前未找到从"+from+"到"+to+" \n的列车。您可以使用出行向导中的\n中转查询功能 查询换乘车次。");
						querybtn.setEnabled(true);
						return;
					}else{
						    int k;
						    JSONObject alldata=JSONObject.fromObject(jsoncontent);
						    JSONArray arraydata=alldata.optJSONArray("data");
						    if(arraydata==null){
						    	logger("很抱歉，按您的查询条件，当前未找到从"+from+"到"+to+" 的列车。您可以使用出行向导中的中转查询功能 查询换乘车次。");
						    	
						    	querybtn.setText(Messages.MainWindow_button_search);
						    	lblNewLabel_resultlog.setText("查询结果：很抱歉，按您的查询条件，\n当前未找到从"+from+"到"+to+" \n的列车。您可以使用出行向导中的\n中转查询功能 查询换乘车次。");
						    	querybtn.setEnabled(false);
								return;
						    }
						    lblNewLabel_resultlog.setText("查询结果：\n正在查询中...");
							int findtrainnumber=arraydata.size();
							for(k=0;k<findtrainnumber;k++){
								   
							    	   JSONObject everytraindata=  arraydata.getJSONObject(k);
							    	
							    	   String securtstr=everytraindata.getString("secretStr");
						    	   
							    	   JSONObject leftnewDTO=everytraindata.getJSONObject("queryLeftNewDTO");		    	
							    	   String station_train_code=leftnewDTO.getString("station_train_code");
							    	   String canwebbuy=leftnewDTO.getString("canWebBuy");
							    	   
							    	   String from_station_name=leftnewDTO.getString("from_station_name");
							    	   String to_station_name=leftnewDTO.getString("to_station_name");
							    	   String start_time=leftnewDTO.getString("start_time");
							    	   String arrive_time=leftnewDTO.getString("arrive_time");
							    	   String lishi=leftnewDTO.optString("lishi");
							    	   String yz_num=leftnewDTO.getString("yz_num");
							    	 //  String intervaltime="";
							    
										
									//	for (int loopIndex = 0; loopIndex <k; loopIndex++) {
										TableItem item = new TableItem(table_list, SWT.NULL);
										     // item.setText("Item " + loopIndex);
										item.setText(new String[]{
												"",station_train_code,from_station_name+" 到  "+to_station_name,
												"出发时间: "+start_time+"，到达时间:"+arrive_time,lishi,
												//TimeUtils.howLong(start_time,arrive_time),
												securtstr,yz_num,canwebbuy
										});
										item.setChecked(false);
							    }
							    lblNewLabel_resultlog.setText("查询结果：\n查询到从"+from+"到"+to+" \n的列车共计 "+k+" 趟班次");
					}   
					querybtn.setText("查询");	
					querybtn.setEnabled(true);	
					logger("本次查询列车信息："+from+"到"+to+"的列车完成！");
				} catch (KeyManagementException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NoSuchAlgorithmException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
				
			}else{
				logger("请输入出发地，目的地和出发时间");
				lblNewLabel_resultlog.setText("查询结果：请输入出发地，目的地和列车\n的出发时间再进行查询...");
				logger("查询返回错误，你需要输入出发地，目的地和出发的时间才能进行查询操作");
				return;
			}
		}
		
	}
	
  class startRunListener implements SelectionListener{
		
		@Override
		public void widgetSelected(SelectionEvent event) {
			// TODO Auto-generated method stub
		    if(btnNewButton_run.getText()==Messages.MainWindow_btnNewButton_runnow){	//是否是run
		    	logger.info("抢票操作进程已经启动......");
		    	
		    	if(!window.islogged){
		    		logger("发现你没有登录12306，请在上面输入你的12306账号进行登录");
		    		return;
		    	}
		    	//String traindate=dateTime_date.getSeconds()
		    	if(text_from.getText().trim().equals("")||text_to.getText().trim().equals("")){
					logger("请从列车车次信息中输入查询列车车次的出发地，目的地和时间");
					return;
				}
		    	if(isrunthread){
		    		logger("朋友，实际上你已经启动了订票的线程，请不要点击按钮再次尝试订票了，好吧！");
		    		return;
		    	}
		    	
		    	String from=text_from.getText().trim();   //出发地
				String to=text_to.getText().trim();      //目的地
				Calendar instance = Calendar.getInstance();  
				instance.set(Calendar.DAY_OF_MONTH, dateTime_date.getDay());
				instance.set(Calendar.MONTH, dateTime_date.getMonth());
				instance.set(Calendar.YEAR, dateTime_date.getYear());
				String traindate = new SimpleDateFormat("yyyy-MM-dd").format(instance.getTime());  //出发时间
				
				boolean isadult=button_adult.isEnabled();
				
				if(isadult){
					strTicketType="ADULT";
				}else{
					strTicketType="0X00";
				}
				
				//得到选择的乘客，列车车次，列车座位类型
				List<PassengerBean> passengerlist=getSelectedPassengers();
				List<String> trainnolist=getSelectedTrains();
				List<String> traintypelist=getSelectedSeatTypes();
				
		    	QueryUserBean querybean=new QueryUserBean(from, to, traindate,strTicketType, passengerlist, trainnolist, traintypelist);
		    	
		    	//设置查询的Cookies		    	
		    	cookies.clear();
				getCookie(cookies, from, to, traindate);
				logger("本次线程用到的Cookies 是："+cookies.toString());
		    	
				RunThread starter=new RunThread(querybean, window);
				starter.start();
		    	btnNewButton_run.setText(Messages.MainWindow_btnNewButton_stoprun); //改变text
		    	//starter.stop();
		    	
		    	
		    }else{	
		    	System.out.println("点击了停止抢票按钮，现在就停止抢票的进程");
		    	window.isstopped=true;
		    	btnNewButton_run.setText(Messages.MainWindow_btnNewButton_runnow);
		    	
		    }
			
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent arg0) {
			// TODO Auto-generated method stub
			
		}

	}
	
	public List<PassengerBean> getSelectedPassengers(){
		List<PassengerBean> selectedpassenger=new LinkedList<PassengerBean>();		
		//如果用户没有选择任何的一个旅客，将选择当前登录用户乘客
		if(table_passengerslist.getItemCount()==0||table_passengerslist==null){			
			try {
				String msg=client.postRequest(UrlConstants.URL_PASSENGER, null, HttpHeader.getPassengerList(), cookies);
								
				JSONObject content=JSONObject.fromObject(msg);
				JSONObject data=content.getJSONObject("data");
				JSONArray normal_passengers=data.getJSONArray("normal_passengers");
				int passengersize=normal_passengers.size();
								
				for(int k=0;k<passengersize;k++){
					JSONObject passenger=normal_passengers.getJSONObject(k);
					
					String passenger_name=passenger.getString("passenger_name");
					String passenger_id_type_code=passenger.getString("passenger_id_type_code");
					String passenger_id_no=passenger.getString("passenger_id_no");
					String passenger_type=passenger.getString("passenger_type");		
					String mobile_no=passenger.getString("mobile_no");
					String email=passenger.getString("email");
					if(email.equals(loginbean.getUsername())){			
						if (StringHelper.isEmptyString(mobile_no)) {
						    selectedpassenger.add(new PassengerBean(passenger_name,passenger_type, passenger_id_no,passenger_id_type_code));								
					    } else {
							selectedpassenger.add(new PassengerBean(passenger_name, passenger_type, passenger_id_no, mobile_no,passenger_id_type_code));									
						}
					}
				}
			 } catch (KeyManagementException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			 } catch (NoSuchAlgorithmException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			 }
		}else{			
			for(int x=0;x<table_passengerslist.getItemCount();x++){					
				if(table_passengerslist.getItem(x).getChecked()){
					String name=table_passengerslist.getItem(x).getText(1);
					String passengertype=table_passengerslist.getItem(x).getText(5);
					String cardid=table_passengerslist.getItem(x).getText(6);
					
					String cardtype=table_passengerslist.getItem(x).getText(7);
					String mobile=table_passengerslist.getItem(x).getText(8);
					PassengerBean singlepassenger;
					if (StringHelper.isEmptyString(cardid) || !StringHelper.isEmptyString(name)) {
							if (!StringHelper.isEmptyString(mobile)) {
								singlepassenger=new PassengerBean(name,passengertype, cardid,cardtype);								
							} else {
								singlepassenger=new PassengerBean(name, passengertype, cardid, mobile,cardtype);									
							}
							selectedpassenger.add(singlepassenger);
					}
				}
			}						
		}
				
		//logger.info("后面我们需要订票的乘客的信息是："+selectedpassenger.toString());
		return selectedpassenger;
	}
	
	public List<String> getSelectedTrains(){
		List<String> trainnolist=new LinkedList<String>();
		for(int x=0;x<table_list.getItemCount();x++){					
			if(table_list.getItem(x).getChecked()){
				String trainno=table_list.getItem(x).getText(1);
				trainnolist.add(trainno);
			}
		}
		return trainnolist;
	}
 
	public List<String> getSelectedSeatTypes(){
			List<String> traintypelist=new LinkedList<String>();
	
			for(int x=0;x<table_type.getItemCount();x++){					
				if(table_type.getItem(x).getChecked()){
					String traintypename=table_type.getItem(x).getText(2);
					traintypelist.add(traintypename);
				}
			}
			//如果遍历完后发现列表还是为空
			if(traintypelist.isEmpty()||traintypelist==null){
				Map<String,String> traintypes=new TicketCode().seattype;
				for(String key:traintypes.keySet()){
					 traintypelist.add(key);
				}
			}
			return traintypelist;
	}
}
